package com.laoyao.comm.config;



import javax.servlet.http.Cookie;

import com.jfinal.aop.Interceptor;
import com.jfinal.aop.Invocation;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.laoyao.util.MoreTime;
import com.laoyao.util.TextUtil;

public class HtAuthLanJie implements Interceptor{

	@Override
	public void intercept(Invocation inv) {
		// TODO 自动生成的方法存根
		System.out.println("Before invoking " + inv.getActionKey());//class路径
		Cookie name=TextUtil.getCookieByName(inv.getController().getRequest(), "username");
		Cookie pwd=TextUtil.getCookieByName(inv.getController().getRequest(), "password");
		String username="";
		String password="";
		if (name!=null) {
			username=name.getValue();
		}
		if (pwd!=null) {
			password=pwd.getValue();
		}
		//保存操作日志操作记录
		String str = inv.getController().getRequest().getQueryString();
		String url = inv.getActionKey() + "?" + str;
		System.out.println("账号："+username+"密码："+password);
		if (str != null &&!str.equals("null")) {
			Record sys=new Record();
			sys.set("name", username).set("ip", TextUtil.getIpAddr(inv.getController().getRequest()))
				.set("url",url).set("time", MoreTime.getTime());
			Db.save("sys_log", sys);
		}else{
			Record sys=new Record();
			sys.set("name", username).set("ip", TextUtil.getIpAddr(inv.getController().getRequest()))
				.set("url",inv.getActionKey()).set("time", MoreTime.getTime());
			Db.save("sys_log", sys);
		}
		if(TextUtil.isEmpty(username)==false && TextUtil.isEmpty(password)==false){
			String sql="select * from admin where admin_tel=? and admin_pwd=? and admin_state=0";
			Record user=Db.findFirst(sql,username,password);
			if (user!=null) {
				//拦截到权限授权提示页面
				inv.invoke();//用户已授权，放过拦截
			}else {
				//拦截到登录页面
				inv.getController().redirect("/login");
			}
		}else{  
			//拦截到登录页面
	        inv.getController().redirect("/login");
		}
	
	}

}
